﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Windows.Forms;
using GW2SE.Base;

namespace GW2SE.LoginServer
{
    class Program
    {
        static void Main(string[] args)
        {
            var NetManServer = new Server();
            var NetManClient = new TcpClient();
            NetManServer.Start(System.Net.IPAddress.Any, 6112);

            Console.WriteLine("Listening...");

            int clientCount = 0;

            Console.Title = "Guild Wars 2 Login Server [Pre-Alpha]";

            while (NetManServer.Running)
            {
                if (NetManServer.Pending)
                {
                    Console.Clear();
                    NetManClient = NetManServer.AcceptClient();
                    clientCount++;
                    Console.WriteLine("Client[" + clientCount.ToString() + "] connected");
                    var connected = true;

                    while (NetManClient.Connected)
                    {
                        try
                        {
                            var packetStream = NetManClient.GetStream();

                            int i;
                            byte[] bytes = new byte[256];

                            while ((i = packetStream.Read(bytes, 0, bytes.Length)) != 0)
                            {
                                Console.WriteLine();
                                Console.Write("Packet: ");
                                Console.WriteLine(BitConverter.ToString(bytes).Replace("-", " "));
                            }
                        }
                        catch (Exception)
                        {
                            if (connected)
                            {
                                if (connected)
                                {
                                    Console.WriteLine("Client disconnected");
                                    connected = false;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
